<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
      思路：何为摆动，摆动即为峰值，所以需要去除单调坡度上节点即可
      即prediff=nums[i]-nums[i-1]   curdiff=nums[i+1]-nums[i]
      if(prediff<0&&curdiff>0||prediff>0&&curdiff<0)即为峰值

      但是需要考虑到三种特殊情况
      ①上下坡中有平坡。可以删除前几个元素  使得prediff<0&&curdiff>=0 || prediff>0||curdiff<=0
      ②需要考虑到数组首尾情况，所以遍历需要集合i<nums.length-1 初始值个数可以默认是1
      ③单调坡度有平坡 [1,2,2,2,3,4] 单调坡度中的波动不算峰值。只有等出现峰值的时候才能替换之前的峰值，不能每次都替换
      */
            var wiggleMaxLength = function (nums) {
                if (nums.length < 2) {
                    return nums.length
                }
                let curdiff = 0
                let prediff = 0
                let result = 1
                for (let i = 0; i < nums.length - 1; i++) {
                    curdiff = nums[i + 1] - nums[i]
                    if ((prediff <= 0 && curdiff > 0) || (prediff >= 0 && curdiff < 0)) {
                        result++
                        prediff = curdiff
                    }
                }
                return result
            }
        </script>
    </body>
</html>
